package projects

import (
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/server/web/pagination"
	"strconv"
	"time"
	"xpms/controllers"
	"xpms/cores"
	"xpms/cores/utils"
)

//  项目团队管理
type TeamProjectController struct {
	controllers.BaseController
}

// @Title 团队
// @Description 分页查询项目团队数据
// @Param	id		path 	string	true		"项目id"
// @Param	p		query 	int	true		"第几页"
// @Param	pageoffset		query 	int			"每页数"
// @Param	keyword		query 	string			"关键字"
// @Success 200 {object} projects.ProjectsTeamDto  int   int  int
// @Failure 403  not exist
// @router /:id/page [get]
func (u *TeamProjectController) GroupByPage() {
	id, err := u.GetInt64(":id")
	if err != nil || id < 0 {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择项目"}
		u.ServeJSON()
		return
	}
	page, err := u.GetInt("p")
	if err != nil {
		page = 1
	}
	offset, err := u.GetInt("pageoffset")
	if err != nil {
		offset = 15
	}
	keyword := u.GetString("keyword")
	condArr := make(map[string]interface{})
	condArr["projectid"] = id
	condArr["keyword"] = keyword
	countGroup := TeamPageCountExec(condArr, page, offset)
	paginator := pagination.SetPaginator(u.Ctx, offset, countGroup)
	modelDbData := TeamPageExec(condArr, page, offset)
	type data struct {
		Teams    []ProjectsTeamDto
		Total    int64
		PageNums int
		Page     int
	}
	d := &data{modelDbData, paginator.Nums(), paginator.PageNums(), paginator.Page()}
	var responseEntity cores.ResponseEntity
	u.Data["json"] = responseEntity.Build(d).Data
	u.Ctx.Output.Status = responseEntity.StatusCode
	u.ServeJSON()
}

type TeamDto struct {
	Projectid int64 `json:"Projectid,string"`
	Role      int   `json:"Role,string"`
	Userids   []string
}

// @Title 添加 team
// @Description add team
// @Param	body		body 	projects.TeamDto	true		"body for TeamDto content"
// @Success 200 {string} success
// @Failure 403 body is empty
// @router / [post]
func (u *TeamProjectController) AddTeam() {
	var dto TeamDto
	fmt.Println(string(u.Ctx.Input.RequestBody[:]))
	_ = json.Unmarshal(u.Ctx.Input.RequestBody, &dto)
	fmt.Println(dto)
	if utils.IsEmpty(dto.Projectid) {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择添加的项目"}
		u.ServeJSON()
		return
	} else if utils.IsEmpty(dto.Userids) {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "没有对应的添加人员"}
		u.ServeJSON()
		return
	}
	var upd []ProjectsTeam
	for _, v := range dto.Userids {
		v1, err := strconv.ParseInt(v, 10, 64)
		exists, err := GetProjectTeam(v1, dto.Projectid)
		if utils.IsEmpty(exists) && err == nil {
			id := utils.SnowFlakeId()
			db := new(ProjectsTeam)
			db.Id = id
			db.Projectid = dto.Projectid
			db.Role = dto.Role
			db.Created = time.Now().Unix()
			db.Userid = v1
			upd = append(upd, *db)
		}
	}
	if upd != nil {
		err := AddMultiTeam(upd)
		if err == nil {
			u.Data["json"] = map[string]interface{}{"code": 1, "message": "操作成功"}
		} else {
			u.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败"}
		}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "用户已存在"}
	}
	u.ServeJSON()
}

// @Title Delete
// @Description delete the team
// @Param	ids	 query 	[]string	true		"The ids you want to delete team id"
// @Success 200
// @Failure 403 id is empty
// @router / [delete]
func (u *TeamProjectController) DeleteGroupUser() {
	ids := u.GetStrings("ids")
	if utils.IsEmpty(ids) {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择要删除的用户"}
		u.ServeJSON()
		return
	}
	err := DeleteTeam(ids)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "删除成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "删除失败"}
	}
	u.ServeJSON()
}
